package hu.rsc.feeds.server;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.w3c.dom.Document;

import com.google.code.facebookapi.FacebookWebappHelper;
import com.google.code.facebookapi.FacebookXmlRestClient;
import com.google.code.facebookapi.IFacebookRestClient;

/**
 * The Facebook User Filter ensures that a Facebook client that pertains to the logged in user is available in the
 * session object named "facebook.user.client".
 * 
 * The session ID is stored as "facebook.user.session". It's important to get the session ID only when the application
 * actually needs it. The user has to authorise to give the application a session key.
 * 
 * @author Dave
 */
public class FacebookUserFilter implements Filter {
    private static String facebookUserId = "id";
    private static String ipAddress = "ip";

    private static final Logger logger = LoggerFactory.getLogger(FacebookUserFilter.class);

    private String api_key;
    private String secret;

    //private static final String FACEBOOK_USER_CLIENT = "facebook.user.client";

    public static HashMap<String, IFacebookRestClient> clients = new HashMap<String, IFacebookRestClient>();

    public static FacebookXmlRestClient getUserClient(HttpSession session) {
	return (FacebookXmlRestClient) clients.get(session.getId());
	//return (FacebookXmlRestClient) session.getAttribute(FACEBOOK_USER_CLIENT);
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
	try {
	    MDC.put(ipAddress, req.getRemoteAddr());

	    HttpServletRequest request = (HttpServletRequest) req;
	    HttpServletResponse response = (HttpServletResponse) res;

	    HttpSession session = request.getSession(true);
	    IFacebookRestClient<Document> userClient = getUserClient(session);
	    if (userClient == null) {
		logger.debug("User session doesn't have a Facebook API client setup yet. Creating one and storing it in the user's session.");
		userClient = new FacebookXmlRestClient(this.api_key, this.secret);
		FacebookUserFilter.clients.put(session.getId(), userClient);
	    }

	    FacebookWebappHelper<Document> facebook = new FacebookWebappHelper<Document>(request, response, this.api_key, this.secret, userClient);

	    /*logger.trace("Creating a FacebookWebappHelper, which copies fb_ request param data into the userClient");
	    FacebookWebappHelper<Document> facebook = new FacebookWebappHelper<Document>(request, response, this.api_key, this.secret, userClient);
	    String nextPage = request.getRequestURI();
	    nextPage = nextPage.substring(nextPage.indexOf("/", 1) + 1); //cut out the first /, the context path and the 2nd /
	    logger.trace(nextPage);
	    boolean redirectOccurred = facebook.requireLogin(nextPage);
	    if (redirectOccurred) {
	    return;
	    }
	    redirectOccurred = facebook.requireFrame(nextPage);
	    if (redirectOccurred) {
	    return;
	    }*/

	    long facebookUserID;
	    try {
		facebookUserID = facebook.getUser();
	    } catch (Exception ex) {
		response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex.getMessage());
		logger.error("Error while getting cached (supplied by request params) value "
			+ "of the user's facebook ID or while fetching it from the Facebook service "
			+ "if the cached value was not present for some reason. Cached value = {}", userClient.getCacheUserId());
		return;
	    }

	    MDC.put(facebookUserId, String.valueOf(facebookUserID));

	    chain.doFilter(request, response);
	} finally {
	    MDC.remove(ipAddress);
	    MDC.remove(facebookUserId);
	}
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
	this.api_key = filterConfig.getServletContext().getInitParameter("facebook_api_key");
	this.secret = filterConfig.getServletContext().getInitParameter("facebook_secret");
	if (this.api_key == null || this.secret == null) {
	    throw new ServletException("Cannot initialise Facebook User Filter because the " + "facebook_api_key or facebook_secret context init "
		    + "params have not been set. Check that they're there " + "in your servlet context descriptor.");
	} else {
	    logger.info("Using facebook API key: " + this.api_key);
	}
    }
}